<?php
require_once("dbConfig.class.php");
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function onChangeAction(select) {
	var dest_path = document.getElementById("dest_path");
	var archive_pwd = document.getElementById("archive_pwd");


	switch(select.value) {
	case "extract":
		dest_path.disabled = true;
		archive_pwd.disabled = false;
	break;
	case "move":
		dest_path.disabled = false;
		archive_pwd.disabled = true;
	break;
	case "extract_move":
		dest_path.disabled = false;
		archive_pwd.disabled = false;
	break;		
	}
}
</script>
<style type="text/css">
#table_insert td {
	font-family:verdana;
	font-size:13px;
	font-weight:bold;
}

.lbl_root_destpath {
	font-family: tahoma;
	font-size:12px;
	font-style:italic;
	font-weight:normal;
	color:gray;
	display:block;
	float:left;
}
</style>
</head>
<body>
<?php

$ed2k_link = $_GET["ed2k_link"];
$destination_path = $_GET["dest_path"];
$action = $_GET["action"];
$archive_pwd = $_GET["archive_pwd"];
$file_name = $_GET["file_name"];
$add_to_queue = $_GET["amulequeue"];
$new_filename = $_GET["new_filename"];

if($_GET["postinsert"] != "true" && ($ed2k_link || $file_name)) {
	if(!$file_name){
		$file_name_parts = explode("|", $ed2k_link);
		$file_name = $file_name_parts[2];
	
	}
	
	if($action != "move" && $action != "extract" && $action != "extract_move") {
		$action = "move";
	}
	
	if($ed2k_link && $add_to_queue == "true") {
		echo add_to_amuled($ed2k_link);
	}

	add($ed2k_link, $file_name, $destination_path, $action, $archive_pwd);
	//echo "Link Inserito correttamente";
	
} else {
	
	mostraform("", $file_name, $ed2k_link, $_GET["postinsert"]);
}

function add_to_amuled($ed2k_link) {
	if($ed2k_link) {
		$output = shell_exec("amulecmd -P=elettrodo1254 -c=\"Add " . $ed2k_link . "\"");
	} else {
		$output = "link ed2k not setted";
	}
	
	return $output;
}

function add($ed2k_link, $file_name, $destination_path, $action, $archive_pwd) {
        $database = parametriDatabase();
        mysql_connect($database["indirizzo"], $database["username"], $database["password"]);
        mysql_select_db($database["nome"]);

	$query = "";
	if($ed2k_link) {
		$query = "select filename from files_catalog where url = '" . str_replace("\'", "''", $ed2k_link) . "'";
	} else if($file_name) {
		$query = "select filename from files_catalog where filename = '" . str_replace("\'", "''", urldecode($file_name)) . "'";
	}
	
	if($query) {
		$rs = mysql_query($query);
		$record = mysql_fetch_array($rs);
		
		if(!$record) {
			$query = "INSERT INTO files_catalog (url, filename, destinationpath, action, filepassword, new_filename, datestart) VALUES ('" . str_replace("\'", "''", $ed2k_link) . "', '" . str_replace("\'", "''", urldecode($file_name)) . "', '$destination_path', '$action', '$archive_pwd', '$new_filename', " . date("YmdHis") . ")";
			mysql_query($query);
			echo "Link aggiunto al catalogo: $ed2k_link<br />";
		} else {
			mostraform("esite già il file \"$file_name\"", "", $ed2k_link, "");
		}
	}
}

function getHistoryDestination() {
	$database = parametriDatabase();
	mysql_connect($database["indirizzo"], $database["username"], $database["password"]);
	mysql_select_db($database["nome"]);

	$query = "select DISTINCT destinationpath from files_catalog ORDER BY destinationpath ASC";
	
	$rs = mysql_query($query);
	
	$options = "<option value=\"\"><i>Percorsi Recenti</i></option>\n";
	
	while($record = mysql_fetch_assoc($rs)) {
		$options .= "<option value=\"" . $record["destinationpath"] . "\">" . $record["destinationpath"] . "</option>\n";
	}
		
	return $options;
}

function mostraform($error, $file_name, $ed2k_link, $postinsert) {
?>
<form action="" method="get">
<input type="hidden" name="file_name" value="<?php echo $file_name; ?>" />
<table id="table_insert" cellpadding="5">
	<tr>
		<td colspan="2" align="center"><?php echo $file_name; ?></td>
	</tr>
	<tr>
		<td>link ed2k:</td><td><input type="text" name="ed2k_link" value="<?php echo $ed2k_link?>" style="width:600px" <?php if($postinsert == "true" && $file_name) { echo "disabled=\"true\""; } ?> /></td>
	</tr>
	<tr>
		<td>Aggiungi in coda:</td><td><input type="checkbox" name="amulequeue" value="true" checked /></td>
	</tr>
	<tr>
		<td>Al completamento:</td>
		<td>
			<select id="action" name="action" onChange="onChangeAction(this)">
				<option value="move">sposta</option>
				<option value="extract_move">estrai e sposta</option>
				<option value="extract">estrai</option>
			</select>
		</td>
	</tr>
	<tr>
		<td></td>
		<td align="left"><span style="padding-left:60px">in</span></td>
	</tr>
	<tr>
		<td></td><td valign="bottom"><div style="float:left; margin-right:20px;"><br /><br /><span class="lbl_root_destpath">/store/shared/ <span></div><div style="float:left"><select id="history_dest_path" name="history_dest_path" onchange="document.getElementById('dest_path').value=this.value; document.getElementById('lbl_destpath').innerHTML = (document.getElementById('dest_path').value + '&nbsp;')"><?php echo getHistoryDestination() ?></select><br /><input id="dest_path" type="text" name="dest_path" value="directory di default" onfocus="if(this.value=='directory di default') { this.value='' }" onblur="if(this.value=='') { this.value='directory di default' }; document.getElementById('lbl_destpath').innerHTML = (this.value.replace(/directory di default/i, '') + '&nbsp;');" onkeyup="document.getElementById('lbl_destpath').innerHTML = (this.value + '&nbsp;')" style="width:300px" /></div></td>
	</tr>
	<tr>
		<td></td>
		<td align="left"><span style="padding-left:60px">rinomina in</span></td>
	</tr>
	<tr>
		<td></td><td><span class="lbl_root_destpath">/store/shared/</span><span id="lbl_destpath" class="lbl_root_destpath" style="min-width:100px !important">&nbsp;</span> <input id="new_filename" type="text" name="new_filename" value="" onfocus="" onblur="" style="width:200px" /></td>
	</tr>		
	
	<tr>
		<td>Password File:</td><td><input id="archive_pwd" type="text" name="archive_pwd" value="" disabled="true" /></td>
	</tr>		
	<tr>
		<td colspan="2" align="center"><input type="submit" value="aggiungi"></input></td>
	</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
